package cn.zyq.ykb.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.task.MyTask;
import cn.zyq.ykb.beans.task.Task;
import cn.zyq.ykb.beans.task.vo.UserCountVo;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.beans.user.bo.SwitchVersionBo;
import cn.zyq.ykb.mapper.task.MyTaskMapper;
import cn.zyq.ykb.mapper.task.TaskMapper;
import cn.zyq.ykb.mapper.user.UserMapper;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.enums.core.ResponseCodeEnum;
import cn.zyq.ykb.system.enums.core.UserPositionEnum;
import cn.zyq.ykb.system.enums.core.promotionTypeEnum;
import cn.zyq.ykb.system.exception.BusinessException;
import cn.zyq.ykb.system.utils.VerifyCodeUtil;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.user.UserPromotionMapper;
import cn.zyq.ykb.beans.user.UserPromotion;
import cn.zyq.ykb.service.user.UserPromotionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户-推广Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-23
 */
@Service
@Slf4j
public class UserPromotionServiceImpl  extends BaseService implements UserPromotionService {

    @Autowired
    private UserPromotionMapper userPromotionMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MyTaskMapper myTaskMapper;

    @Autowired
    private TaskMapper taskMapper;

    /**
     * 查询用户-推广
     *
     * @param userPromotionID 用户-推广主键
     * @return 用户-推广
     */
    @Override
    public UserPromotion selectUserPromotionByUserPromotionID(Integer userPromotionID) {
        UserPromotion userPromotion = userPromotionMapper.selectByPrimaryKey(userPromotionID);
        if (userPromotion.getDeleted() == 1) {
            return null;
        }
        return userPromotion;
    }

    /**
     * 查询用户-推广
     *
     * @param serviceID 业务ID
     * @return 用户-推广
     */
    @Override
    public UserPromotion selectUserPromotionByServiceID(String serviceID) {
        UserPromotion param = new UserPromotion();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.userPromotionMapper.selectOne(param);
    }

    /**
     * 查询用户-推广列表
     *
     * @param pageParam
     * @return 用户-推广
     */
    @Override
    public PageInfo<UserPromotion> selectUserPromotionList(PageParam pageParam, User user) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        pageParam.getKv().put("userID",user.getUserID());
        List<UserPromotion> list = this.userPromotionMapper.list(pageParam.getKv());
        for (UserPromotion userPromotion : list) {
            User user1 = this.userMapper.selectByPrimaryKey(userPromotion.getPUser());
            userPromotion.setUser(user1);
            User user2 = this.userMapper.selectByPrimaryKey(userPromotion.getUserID());
            userPromotion.setTUser(user2);
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<UserPromotion> selectUserPromotionList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<UserPromotion> list = this.userPromotionMapper.selectUserPromotionList(pageParam.getKv());
        for (UserPromotion userPromotion : list) {
            User user1 = this.userMapper.selectByPrimaryKey(userPromotion.getPUser());
            userPromotion.setUser(user1);
            User user2 = this.userMapper.selectByPrimaryKey(userPromotion.getUserID());
            userPromotion.setTUser(user2);
        }
        return new PageInfo<>(list);
    }

    /**
     * 查询用户-推广列表
     *
     * @param param
     * @return 用户-推广
     */
    @Override
    public List<UserPromotion> selectUserPromotionList(UserPromotion param) {
        param.setDeleted(0);
        return this.userPromotionMapper.select(param);
    }
    /**
     * 新增用户-推广
     *
     * @param param 用户-推广
     * @return 结果
     */
    @Override
    public int insertUserPromotion(UserPromotion param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.userPromotionMapper.insertSelective(param);
    }

    /**
     * 修改用户-推广
     *
     * @param param 用户-推广
     * @return 结果
     */
    @Override
    public int updateUserPromotion(UserPromotion param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.userPromotionMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除用户-推广
     *
     * @param userPromotionIDs 需要删除的用户-推广主键
     * @return 结果
     */
    @Override
    public int deleteUserPromotionByUserPromotionIDs(Integer[] userPromotionIDs) {
        int i = 0;
        for (Integer userPromotionID : userPromotionIDs) {
            i += this.deleteUserPromotionByUserPromotionID(userPromotionID);
        }
        return i;
    }

    /**
     * 删除用户-推广信息
     *
     * @param userPromotionID 用户-推广主键
     * @return 结果
     */
    @Override
    public int deleteUserPromotionByUserPromotionID(Integer userPromotionID) {
        UserPromotion userPromotion = this.selectUserPromotionByUserPromotionID(userPromotionID);
        if (userPromotion == null) {
            return 0;
        } else {
            UserPromotion storeInfo = new UserPromotion();
            storeInfo.setUserPromotionID(userPromotionID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.userPromotionMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    @Transactional
    public int switchVersion(SwitchVersionBo bo, User loginUser) {
        log.info("入参：{}",bo);
        if (CollectionUtils.isEmpty(bo.getServiceIDs())){
            throw new BusinessException("用户ID必传");
        }
        for (String server : bo.getServiceIDs()) {
            UserPromotion param = this.selectUserPromotionByServiceID(server);
            if (BeanUtil.isEmpty(param)){
                throw new BusinessException(ResponseCodeEnum.ERROR,"找不到当前用户信息");
            }
            param.setPromotionType(promotionTypeEnum.XSB.getValue());

            User user = this.userMapper.selectByPrimaryKey(param.getPUser());
            if (BeanUtil.isEmpty(user)){
                throw new BusinessException(ResponseCodeEnum.ERROR,"找不到该推广人的用户信息");
            }
            if (bo.getState().equals(promotionTypeEnum.XSB.getValue())) {
                user.setPosition(UserPositionEnum.PT.getValue());
                param.setPosition(UserPositionEnum.PT.getValue());
            }else if (bo.getState().equals(promotionTypeEnum.ZSB.getValue())){
                user.setPosition(UserPositionEnum.TDZ.getValue());
                param.setPosition(UserPositionEnum.TDZ.getValue());
            }else {
                throw new BusinessException(ResponseCodeEnum.ERROR,"切换的用户类型异常");
            }
            user.setCode(VerifyCodeUtil.generateVerifyCode(6));
            this.setUpdateBaseData(user, loginUser);
            int i1 = this.userMapper.updateByPrimaryKeySelective(user);
            if (i1 == 0){
                log.info("修改用户表状态失败");
                return 0;
            }
            this.setUpdateBaseData(param, loginUser);
            int i = this.userPromotionMapper.updateByPrimaryKeySelective(param);
            if (i==0){
                log.info("修改用户推广表状态失败");
                return 0;
            }
        }
        return 1;
    }

    @Override
    public List<MyTask> taskList(PageParam pageParam, User loginUser) {
        pageParam.getKv().put("userID",loginUser.getUserID());
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<MyTask> myTasks = this.myTaskMapper.taskList(pageParam.getKv());
        for (MyTask myTask : myTasks) {
            Task task = this.taskMapper.selectByPrimaryKey(myTask.getTaskID());
            myTask.setTask(task);
            User user = this.userMapper.selectByPrimaryKey(myTask.getUserID());
            myTask.setUser(user);
        }
        return myTasks;
    }

    @Override
    public UserCountVo getCount(User loginUser, String createTime, Integer type) {
        if (type == null ){
            throw new BusinessException(ResponseCodeEnum.ERROR,"请参type参数");
        }
        if (type.equals(1)){
            return this.userPromotionMapper.getCount1(loginUser.getUserID());
        }else {
            return this.userPromotionMapper.getCount(loginUser.getUserID(),createTime);
        }

    }

    @Override
    public UserCountVo getSum(Integer promotionType, Integer type, User loginUser) {
        log.info("类型：{}",promotionType);
        UserCountVo sum = new UserCountVo();
        if (type.equals(1)){
            sum = this.userPromotionMapper.getSum(promotionType, loginUser.getUserID());
        }else {
            sum = this.userPromotionMapper.getSum1(promotionType, loginUser.getUserID());
        }
        return sum;
    }
}
